/**
 * 用户登陆
 */
//登陆
function Login(){
	var isLogin = false;
	var username = InputNotNull($('#username'),"账号不能为空！");
	var password = InputNotNull($('#password'),"密码不能为空！");
	if (username && password) {
		//判断密码是否为自动填充的md5密文
		if($('#password').val().length!==32){
			$('#password').val($.md5($('#password').val()));
		}
		$.ajax({
			type:"post",
			url:"user/login.do",
			datatype: "json",
			async:false,
			data:{
				"account":$("#username").val(),
				"password":$("#password").val(),
			},
			success:function(result) {
				if(result.isLogin===true){ //账号密码正确
					//检查用户是否已经在线
					$.ajax({
						type:"post",
						url:"user/userOnline.do",
						datatype: "json",
						async:false,
						data:{
							"username":$("#username").val(),
						},
						success:function(result){
							//用户已经在线
							if(result.isOnline==true){
								swal({
									  title: "用户已在线",
									  text: result.errorMes,
									  type: "warning",
									  showCancelButton: true,
									  confirmButtonColor: "#DD6B55",
									  confirmButtonText: "强制登录",
									  cancelButtonText: "取消登录",
									  closeOnConfirm: true
									},
									function(isConfirm){//强制登陆
										if (isConfirm) {
											/* 强制账号密码登录 */
											$.ajax({
												type:"post",
												url:"user/userLoginSucceed.do",
												datatype: "json",
												async:false,
												data:{
													"username":$("#username").val(),
													"rememberPsw":$("#rememberPsw").is(':checked'),
												},
												success:function(result) {
													/* 强制其他客户端用户下线 */
													$.ajax({
														type:"post",
														url:"user/userForcedOut.do",
														datatype: "json",
														async:true,
														data:{
															"username":$("#username").val(),
														},
														success:function(result){
															if(result.isOK==true){//强制下线成功
																isLogin = true;
																setInterval(function(){ window.location.href="index.do"; }, 500);
															}else{
																ErrorTipBottomCenter('强制下线失败');
															}
														},
														error:function(){
															AjaxError();
														}
													});
												},
												error:function(){
													AjaxError();
												}
											});
										}
									});
							}
							else{//用户未在线
								$.ajax({
									type:"post",
									url:"user/userLoginSucceed.do",
									datatype: "json",
									async:false,
									data:{
										"username":$("#username").val(),
										"rememberPsw":$("#rememberPsw").is(':checked'),
									},
									success:function(result) {
										isLogin = true; 
									},
									error:function(){
										AjaxError();
									}
								})
							}
						},
						error:function(){
							AjaxError();
						}
					})
				}else{//账号或密码错误
					$('#password').val('');
					ErrorTipBottomCenter(result.errorMes);
				}
			},
			error:function(){
				AjaxError();
			}
		});
	}
	return isLogin;
}

//页面加载完毕
$(function () {
	$('#loginBtn').on('click',function(){
		var requestPath = $('#requestPath').val();
		var islogin = Login();
		if(islogin==true){
			swal("success", "登陆成功!", "success");
			/* 判断是否是从过滤器而来 */
			if(requestPath.length==0){
				setInterval(function(){ window.location.href="index.do"; }, 500);
				/*if(userType==1)window.location.href="/STSM/user/mycenter";
				if(userType==8)window.location.href="/STSM/admin";*/
			}else{
				window.location.href=requestPath;
			}
		}
	})

})